"use strict";
/**
 * @name logger
 * 用户行为统计
 */

/**
 * @name /
 * @param int sid 当前登录服务器ID
 * @param int time 日志收集时间
 * @param string access 后台SDK授权
 * @param json data 上报数据{'openShop':10,'battle':100}
 */

module.exports = function() {
    let sid = this.get('sid','int');
    let time = this.get('time','int');
    let data = this.get('data','json') ;
    let access = this.get('access','string') ;
    if ( !sid || !time || !data || !access ) {
        return this.error('args empty');
    }

    let query = {"access":access};
    let option = {multi:false,fields:{'openid':1} };
    let openidModel = this.model.branch("openid",this.branch);
    return openidModel.find(query,option).then(ret=>{
        if(!ret){
            return this.error("access not exist");
        }
        if( !ret['openid'] ){
            return this.error("openid empty");
        }
    }).then(()=>{
        let loggerModel = this.model.branch("logger",this.branch,sid);
        return loggerModel.incr(time,data);
    })

    
}